import { Body, Controller, UseInterceptors } from '@nestjs/common/decorators';
import {
  Crud,
  CrudController,
  CrudRequest,
  CrudRequestInterceptor,
  Override,
  ParsedRequest,
} from '@dataui/crud';
import { User } from '@/user/entities/user.entity';
import { UserService } from '@/user/user.service';
import { AllowNoToken } from '@/decorators/token.decorator';
import { CreateUserDto } from '@/user/dto/create-user.dto';
import { Get, Post } from '@nestjs/common';
import { CreatePermissionDto } from '@/permission/dto/create-permission.dto';
import { getPassword } from '@/utils/password';

@Crud({
  model: {
    type: User,
  },
  dto: {
    create: CreateUserDto,
  },
})
// @CrudAuth({
//   property: 'user',
//   filter: (user: User) => ({
//     userId: user.id,
//   }),
// })
@Controller('user')
export class UserController implements CrudController<User> {
  constructor(public service: UserService) {}

  // @AllowNoToken()
  // @UsePipes(new ValidationPipe({ transform: true }))
  // @Post('register')
  // login(@ParsedRequest() req, @Body() dto: User): Promise<User> {
  //   return this.service.createOne(req, dto);
  // }

  @Override()
  // @UsePipes(new ValidationPipe({ transform: true }))
  @AllowNoToken()
  async createOne(
    @ParsedRequest() req: CrudRequest,
    @Body() dto: CreateUserDto,
  ): Promise<User> {
    return this.service.createOne(req, dto);
  }

  @UseInterceptors(CrudRequestInterceptor)
  @Get('permission')
  @AllowNoToken()
  handlePermission(
    @ParsedRequest() req: CrudRequest,
    @Body() dto: CreateUserDto,
  ): Promise<any> {
    console.log(req, dto);
    // return this.service.handleRoleAndPermission(req, dto);
    // return this.service.initUsers(req, dto);
    // return this.service.initRole();
    return this.service.addAdminRole();
  }

  @UseInterceptors(CrudRequestInterceptor)
  @Post('setPerm')
  @AllowNoToken()
  addPermission(
    @ParsedRequest() req: CrudRequest,
    @Body() dto: CreatePermissionDto,
  ): Promise<any> {
    return this.service.addPermission(req, dto);
  }

  @UseInterceptors(CrudRequestInterceptor)
  @Get('addCityUsersRole')
  @AllowNoToken()
  addCityUsersRole(
    @ParsedRequest() req: CrudRequest,
    @Body() dto: CreatePermissionDto,
  ): Promise<any> {
    return this.service.addCityUsersRole(req, dto);
  }

  @UseInterceptors(CrudRequestInterceptor)
  @Get('addCityUsers')
  @AllowNoToken()
  addCityUsers(
    @ParsedRequest() req: CrudRequest,
    @Body() dto: CreatePermissionDto,
  ): Promise<any> {
    return this.service.addCityUsers(req, dto);
  }

  @Get('addTestMachine')
  @AllowNoToken()
  addTestMachine(): Promise<any> {
    return this.service.addTestingMachine();
  }
}
